package com.alibaba.handler.execption;

import com.alibaba.common.ResponseResult;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import static com.alibaba.define.enums.AppHttpCodeEnum.FILE_TOO_LARGE;

/**
 * @author : hanyang
 * @date : 2023-11-05 16:09
 * 全局异常处理-文件大小超限返回
 **/

@ControllerAdvice
public class FileUploadExceptionAdvice {

    @ExceptionHandler(SizeLimitExceededException.class)
    public ResponseEntity<ResponseResult<Void>> handleFileSizeLimitExceed(SizeLimitExceededException e) {
        ResponseResult<Void> error = ResponseResult.error(FILE_TOO_LARGE.getCode(), FILE_TOO_LARGE.getMsg());
        return ResponseEntity.status(HttpStatus.PAYLOAD_TOO_LARGE)
                .body(error);
    }
}
